package com.mhs.services;

import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.mhs.DTO.HLCDTO;
import com.mhs.DTO.UserDTO;

public class ServiceSync {
	private static String WSDL_TARGET_NAMESPACE = PublicVariable
			.getWsdlTargetNamespace();

	private static String SOAP_ADDRESS = PublicVariable.getSoapAddress();

	public static SoapObject InvokeMethod(UserDTO user) {
		String SOAP_ACTION = "http://tempuri.org/getRequestSyncFromClient";
		String OPERATION_NAME = "getRequestSyncFromClient";
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);

		if (!user.getUserName().isEmpty() && !user.getIMEI().isEmpty()) {
			PropertyInfo pi = new PropertyInfo();
			pi.setName("USER");
			pi.setValue(user);
			pi.setType(UserDTO.class);
			request.addProperty(pi);
		}

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		envelope.addMapping(WSDL_TARGET_NAMESPACE, "UserDTO",
				new UserDTO().getClass());
		// Log.d("Body", envelope.bodyOut.toString());
		HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_ADDRESS);
		SoapObject response = null;
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			// KvmSerializable ks = (KvmSerializable)envelope.bodyIn;
			// for(int i = 0;i < ks.getPropertyCount();i++)
			// {
			// ks.getProperty(i); //if complex type is present then you can cast
			// this to SoapObject and if primitive type is returned you can use
			// toString() to get actuall value.
			// }
			// response = (SoapObject) ks;
			response = (SoapObject) envelope.getResponse();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return response;
	}

	public static SoapObject requestSyncUpdate(Vector<HLCDTO> lstIR) {
		String SOAP_ACTION = "http://tempuri.org/getRequestUpdateFromClient";
		String OPERATION_NAME = "getRequestUpdateFromClient";

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);

		request.addProperty("lstIR", lstIR);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		// envelope.addMapping(WSDL_TARGET_NAMESPACE, "UserDTO",new
		// UserDTO().getClass());
		HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_ADDRESS);
		SoapObject response = null;
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			response = (SoapObject) envelope.getResponse();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return response;
	}

	public static SoapObject requestDataUpdate(int t) {
		String SOAP_ACTION = "http://tempuri.org/getRequestDataFromClient";
		String OPERATION_NAME = "getRequestDataFromClient";

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);

		if (t != 0) {
			PropertyInfo pi = new PropertyInfo();
			pi.setName("t");
			pi.setValue(t);
			pi.setType(Integer.class);
			request.addProperty(pi);
		}

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		// envelope.addMapping(WSDL_TARGET_NAMESPACE, "UserDTO",new
		// UserDTO().getClass());
		HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_ADDRESS);
		SoapObject response = null;
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			response = (SoapObject) envelope.getResponse();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return response;
	}

	public static SoapObject requestSyncDataUpdate(UserDTO user, Vector<HLCDTO> iR) {
		String SOAP_ACTION = "http://tempuri.org/getRequestUpdateFromClient";
		String OPERATION_NAME = "getRequestUpdateFromClient";

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);
		if (iR != null && user != null) {     
			request.addProperty("username", user.getUserName());
			request.addProperty("password", user.getPassWord());
			SoapObject lstIR = new SoapObject(WSDL_TARGET_NAMESPACE, "lstIR");
			for(int i = 0; i < iR.size(); i++){
				HLCDTO ir = iR.elementAt(i);
				SoapObject HLCDTO = new SoapObject(WSDL_TARGET_NAMESPACE, "HLCDTO");
				HLCDTO.addProperty("MaHLC", ir.getMa());
				HLCDTO.addProperty("TenBang", ir.getTable());
				HLCDTO.addProperty("Command", ir.getCommand());
				lstIR.addProperty("HLCDTO", HLCDTO);
			}
			request.addProperty("lstIR", lstIR);
		}

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_ADDRESS);
		SoapObject response = null;
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			
			response = (SoapObject) envelope.getResponse();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return response;
	}

	public static SoapObject sendConfirmSyncedData(UserDTO user, Vector<HLCDTO> vExistItems, Vector<HLCDTO> vErrorItems) {
		String SOAP_ACTION = "http://tempuri.org/confirmSyncedData";
		String OPERATION_NAME = "confirmSyncedData";

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);
		if (vExistItems != null && user != null && vErrorItems != null) {     
			request.addProperty("username", user.getUserName());
			request.addProperty("password", user.getPassWord());
			///////////////////////////////////////////////////////////////////////////////
			SoapObject soExistItems = new SoapObject(WSDL_TARGET_NAMESPACE, "existItems");
			for(int i = 0; i < vExistItems.size(); i++){
				HLCDTO ir = vExistItems.elementAt(i);
				SoapObject HLCDTO = new SoapObject(WSDL_TARGET_NAMESPACE, "HLCDTO");
				HLCDTO.addProperty("MaHLC", ir.getMa());
				HLCDTO.addProperty("TenBang", ir.getTable());
				HLCDTO.addProperty("Command", ir.getCommand());
				soExistItems.addProperty("HLCDTO", HLCDTO);
			}
			request.addProperty("existItems", soExistItems);
			///////////////////////////////////////////////////////////////////////////////
			SoapObject soErrorItems = new SoapObject(WSDL_TARGET_NAMESPACE, "errorItems");
			for(int i = 0; i < vErrorItems.size(); i++){
				HLCDTO ir = vErrorItems.elementAt(i);
				SoapObject HLCDTO = new SoapObject(WSDL_TARGET_NAMESPACE, "HLCDTO");
				HLCDTO.addProperty("MaHLC", ir.getMa());
				HLCDTO.addProperty("TenBang", ir.getTable());
				HLCDTO.addProperty("Command", ir.getCommand());
				soErrorItems.addProperty("HLCDTO", HLCDTO);
			}
			request.addProperty("errorItems", soErrorItems);
			
		}

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_ADDRESS);
		SoapObject response = null;
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			KvmSerializable ks = (KvmSerializable)envelope.bodyIn;
			for(int i = 0;i < ks.getPropertyCount();i++){
				ks.getProperty(i);
			}
			response = (SoapObject) ks;
			//response = (SoapObject) envelope.getResponse();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return response;
	}
}
